package edu.xjtlu.mobilewebservices.webservice;

import java.io.IOException;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;


import edu.xjtlu.mobilewebservices.R;
import edu.xjtlu.mobilewebservices.history.HistoryList;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;

public class Barcode2WS extends ListActivity {
	private static final String URL = "http://10.7.6.96:8080/server4ws/services/WS4Barcode?wsdl";
	private static final String NAMESPACE = "http://products.webservices.xjtlu.edu";
	private static final String METHOD_NAME = "productsQuery";
	private static final String SOAP_ACTION =METHOD_NAME+NAMESPACE;
	private String code, type, result_list;
	private String[] info;

	/** Called when the activity is first created. */
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//setContentView(R.layout.wssoap);
		
		Bundle bundle = this.getIntent().getExtras();
		code = bundle.getString("key_search_item");
		type = bundle.getString("key_search_type");
		
		//request methods and parameter of web service 
		SoapObject request = new SoapObject(NAMESPACE,METHOD_NAME);
		request.addProperty("search_item",code);
		request.addProperty("search_type",type);
        Log.v("SoapObject", "Ok----SoapObject----");
		
		//define the envelope of sending message
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
		envelope.bodyOut = request;
		Log.v("SoapSerializationEnvelope", "Ok----SoapSerializationEnvelope----");
		

		//construct sending object
		HttpTransportSE htSE = new HttpTransportSE(URL);
		htSE.debug=true;
		Log.v("HttpTransportSE", "Ok----HttpTransportSE----");
	
		
		//call web service for searching with methods name and parameter
			try {
				htSE.call(SOAP_ACTION, envelope);
				Log.v("htSE.call", "Ok----htSE.call()----");
			} catch (IOException e) {
				e.printStackTrace();
			} catch (XmlPullParserException e) {
				e.printStackTrace();
			}

			try {
				if(envelope.getResponse()!=null){
					Log.v("envelope.getResponse()", "Ok----envelope.getResponse()----");
					
					//get the response of web service
					Object result = envelope.getResponse();
					
					//parse the response message
					String st=result.toString();
					result_list=st.substring(1, st.length()-1);
					if (st.substring(1, st.length()-1).split(",").length >= 1){
						info=st.substring(1, st.length()-1).split(",");
					}
					else
						{ info[0]=st;}
					Log.v("envelope.getResponse()", st);
					
					ListAdapter adapter = createAdapter();
				    setListAdapter(adapter);
				}
			} catch (SoapFault e) {
				e.printStackTrace();
			}		
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		this.getMenuInflater().inflate(R.menu.save_history, menu);
         return true;
	}
	
	@Override
    public boolean onOptionsItemSelected(MenuItem item) {
	int item_id=item.getItemId();
	
	switch(item_id){
	
	//put response message into bundle and send to next activity
		case R.id.save_history:{
			Intent intent_result =new Intent(Barcode2WS.this,HistoryList.class);
			Bundle bundle_result =new Bundle();
			bundle_result.putString("product_info", result_list);
			bundle_result.putString("key_search_item", code);
			bundle_result.putString("key_search_type", type);
			intent_result.putExtras(bundle_result);
			startActivity(intent_result);
		}
	}
	return true;
    }
	
	protected ListAdapter createAdapter(){
	ListAdapter adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,info);
	return adapter;
	
}
                                      
}
